package com.pearadmin.shop.controller;

import com.github.pagehelper.PageInfo;
import com.pearadmin.shop.domain.ShopCombo;
import com.pearadmin.common.tools.string.Convert;
import com.pearadmin.common.web.base.BaseController;
import com.pearadmin.common.web.domain.request.PageDomain;
import com.pearadmin.common.web.domain.response.Result;
import com.pearadmin.common.web.domain.response.module.ResultTable;
import com.pearadmin.common.tools.secure.SecurityUtil;
import com.pearadmin.system.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import com.pearadmin.shop.service.IShopComboService;

import java.time.LocalDateTime;

/**
 * 店铺套餐Controller
 *
 * @author syl
 * @date 2021-05-06
 */
@RestController
@RequestMapping("/shop/combo")
public class ShopComboController extends BaseController
{
    private String prefix = "shop/combo";

    @Autowired
    private IShopComboService shopComboService;

    @GetMapping("/main")
    @PreAuthorize("hasPermission('/shop/combo/main','shop:combo:main')")
    public ModelAndView main()
    {
        return jumpPage(prefix + "/main");
    }

    /**
     * 查询店铺套餐列表
     */
    @ResponseBody
    @GetMapping("/data")
    @PreAuthorize("hasPermission('/shop/combo/data','shop:combo:data')")
    public ResultTable list(@ModelAttribute ShopCombo shopCombo, PageDomain pageDomain)
    {
        PageInfo<ShopCombo> pageInfo = shopComboService.selectShopComboPage(shopCombo,pageDomain);
        return pageTable(pageInfo.getList(),pageInfo.getTotal());
    }

    /**
     * 新增店铺套餐
     */
    @GetMapping("/add")
    @PreAuthorize("hasPermission('/shop/combo/add','shop:combo:add')")
    public ModelAndView add()
    {
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增保存店铺套餐
     */
    @ResponseBody
    @PostMapping("/save")
    @PreAuthorize("hasPermission('/shop/combo/add','shop:combo:add')")
    public Result save(@RequestBody ShopCombo shopCombo)
    {
        SysUser sysUser = (SysUser)SecurityUtil.currentUserObj();
        shopCombo.setCreateTime(LocalDateTime.now());
        shopCombo.setCreateBy(sysUser.getUserId());
        shopCombo.setCreateName(sysUser.getUsername());
        return decide(shopComboService.insertShopCombo(shopCombo));
    }

    /**
     * 修改店铺套餐
     */
    @GetMapping("/edit")
    @PreAuthorize("hasPermission('/shop/combo/edit','shop:combo:edit')")
    public ModelAndView edit(Long id, ModelMap mmap)
    {
        ShopCombo shopCombo = shopComboService.selectShopComboById(id);
        mmap.put("shopCombo", shopCombo);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改保存店铺套餐
     */
    @ResponseBody
    @PutMapping("/update")
    @PreAuthorize("hasPermission('/shop/combo/edit','shop:combo:edit')")
    public Result update(@RequestBody ShopCombo shopCombo)
    {
        SysUser sysUser = (SysUser)SecurityUtil.currentUserObj();
        shopCombo.setUpdateTime(LocalDateTime.now());
        shopCombo.setUpdateBy(sysUser.getUserId());
        shopCombo.setUpdateName(sysUser.getUsername());
        return decide(shopComboService.updateShopCombo(shopCombo));
    }

    /**
     * 删除店铺套餐
     */
    @ResponseBody
    @DeleteMapping( "/batchRemove")
    @PreAuthorize("hasPermission('/shop/combo/remove','shop:combo:remove')")
    public Result batchRemove(String ids)
    {
        return decide(shopComboService.deleteShopComboByIds(Convert.toStrArray(ids)));
    }

    /**
     * 删除
     */
    @ResponseBody
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("hasPermission('/shop/combo/remove','shop:combo:remove')")
    public Result remove(@PathVariable("id") Long id)
    {
        return decide(shopComboService.deleteShopComboById(id));
    }
}
